package com.shuma.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuma.common.exception.BusinessException;
import com.shuma.common.exception.BusinessExceptionEnum;
import com.shuma.common.vo.PageResp;
import com.shuma.common.vo.R;
import com.shuma.product.mapper.UserMapper;
import com.shuma.product.mapper.UserProxyMapper;
import com.shuma.product.pojo.User;
import com.shuma.product.pojo.UserProxy;
import com.shuma.product.service.UserProxyService;
import com.shuma.product.vo.UserProxyResp;
import com.shuma.product.vo.req.AddProxyerReq;
import com.shuma.product.vo.req.ProxyerQueryReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86176
* @description 针对表【user_proxy(用户代理人表)】的数据库操作Service实现
* @createDate 2024-06-17 16:15:32
*/
@Service
public class UserProxyServiceImpl extends ServiceImpl<UserProxyMapper, UserProxy>
    implements UserProxyService{

    private static final Logger LOG = LoggerFactory.getLogger(UserProxyServiceImpl.class);


    @Autowired
    private UserProxyMapper userProxyMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public R queryProxyerList(ProxyerQueryReq req) {
        LambdaQueryWrapper<UserProxy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserProxy::getManagerId, req.getUserId());
        queryWrapper.eq(UserProxy::getProductId, req.getProductId());

        LOG.info("查询页码{}", req.getPage());
        LOG.info("每页条数{}", req.getPage());

        // 分页查询
        Page<UserProxy> page = new Page<>(req.getPage(), req.getSize());
        Page<UserProxy> userProxyPage = userProxyMapper.selectPage(page, queryWrapper);

        LOG.info("总条数{}", userProxyPage.getTotal());
        LOG.info("总页数{}", userProxyPage.getPages());

        // 将从数据库查的对象转换为UserProxyResp对象，并封装到列表里面
        List<UserProxyResp> list = BeanUtil.copyToList(userProxyPage.getRecords(), UserProxyResp.class);

        PageResp<UserProxyResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(userProxyPage.getTotal());
        pageResp.setPage(req.getPage());
        pageResp.setSize(req.getSize());
        return R.ok(pageResp);
    }

    @Override
    public R addProxyer(AddProxyerReq req) {
        // 先根据用户名查询该代理人是否存在(这里可以考虑rpc调用)
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, req.getProxyerName());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new BusinessException(BusinessExceptionEnum.USER_NOT_EXIST);
        }

        // 自己代理自己
        if (req.getManagerId() == user.getUserId()) {
            throw new BusinessException(BusinessExceptionEnum.PROXYER_EXIST);
        }


        // 该用户已经是代理人
        LambdaQueryWrapper<UserProxy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserProxy::getProxyerId, user.getUserId());
        wrapper.eq(UserProxy::getManagerId, req.getManagerId());
        Integer count = userProxyMapper.selectCount(wrapper);
        if (count == 1) {
            throw new BusinessException(BusinessExceptionEnum.PROXYER_EXIST);
        }

        UserProxy userProxy = new UserProxy();
        userProxy.setManagerId(req.getManagerId());
        userProxy.setProxyerId(user.getUserId());
        userProxy.setProductId(req.getProductId());
        userProxy.setProxyerName(req.getProxyerName());
        userProxyMapper.insert(userProxy);
        return R.ok();
    }

    @Override
    public R delProxyer(UserProxy req) {
        LambdaQueryWrapper<UserProxy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserProxy::getProxyerId, req.getProxyerId());
        wrapper.eq(UserProxy::getManagerId, req.getManagerId());
        wrapper.eq(UserProxy::getProductId, req.getProductId());
        userProxyMapper.delete(wrapper);
        return R.ok();
    }
}




